package com.fy.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

@Slf4j
public class DateFormatter {
    public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 将CST格式日期String转换为YYMMDDHHMMSS格式String
     *
     * @param CSTTimeString
     * @return
     */
    public static String getYMDhmsFormatterStringByCST(String CSTTimeString){
        SimpleDateFormat sim1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        SimpleDateFormat sim2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=null;
        try {
            date = sim1.parse(CSTTimeString);
        }catch (ParseException e ){
            log.info("日期格式转换异常");
        }
        return sim2.format(date);
    }

    /**
     * 获取当前时间的字符串表示
     *
     * @return 当前时间的字符串表示，格式为 "yyyy-MM-dd HH:mm:ss"
     */
    public static String getNowDateTimeString() {
        LocalDateTime now = LocalDateTime.now();
        return now.format(formatter);
    }

    //加一个方法：把Timestamp日期格式转换变成字符串，格式如下：yyyy-MM-dd HH:mm:ss
    public static String getTimestampStringByDate(Date date){
        SimpleDateFormat sim2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sim2.format(date);
    }
}